﻿using System;
using System.Web.UI.WebControls;
using Pharmacy.Entities;
using Pharmacy.Data;

public partial class Admin_GrnImports : System.Web.UI.Page
{
    string PoId = "";
    string GRNId = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            WhereClauseHiddenField.Value = GetWhereClause();
            
        }
        
    }
    protected string GetWhereClause()
    {
        GRNId = Request.QueryString["GRNId"];
        Grn objGrn = DataRepository.GrnProvider.GetByGrnId(GRNId);
        PoId = objGrn.PoId;
        string WhereClause = "POID= '" + PoId + "'";
        return WhereClause;
    }
    protected void btnImports_Click(object sender, EventArgs e)
    {
        string DrugId = string.Empty;
        Int64 poDetailId = 0;
        int Quantity = 0;
        int PoQuantity = 0;
        int TotalQuantity = 0;
        int TotalPoQuantity = 0;
        decimal Amount = 0;
        decimal TotalAmount = 0;
        decimal unitPrice = 0;
        TextBox txtQuantity;
        TextBox txtCosprice; 
        TextBox txtSellPrice;
        TextBox txtexpdate;
        TextBox txtBatch;
        int count = GridView1.Rows.Count;
        string TextHd = "";
        GridViewRow gvr;
        HiddenField hd;
         HiddenField hdDrug;
        GrnDetails objGrnDetail;
        PoDetails objPoDetail;
        GRNId = Request.QueryString["GRNId"];
        for (int i = 0; i < count; i++)
        {
            
            gvr = GridView1.Rows[i];
            hd = (HiddenField)gvr.FindControl("HFdelete");
            TextHd = hd.Value;
            //get value
            poDetailId = Int64.Parse(GridView1.DataKeys[gvr.RowIndex].Value.ToString());
            hdDrug = (HiddenField)gvr.FindControl("HFDrugId");//DrugId
            DrugId = hdDrug.Value;
            txtQuantity = (TextBox)gvr.FindControl("TxtQuantity");
            txtCosprice = (TextBox)gvr.FindControl("TxtCostPrice");
            txtSellPrice = (TextBox)gvr.FindControl("TxtSellPrice");

            txtBatch = (TextBox)gvr.FindControl("TxtBatchLot");
            txtexpdate = (TextBox)gvr.FindControl("Txtexpdate");
            DateTime? expDate; DateTime dt;
            expDate = DateTime.TryParse(txtexpdate.Text, out dt) ? dt : (DateTime?)null;
            Quantity = int.Parse(txtQuantity.Text); //quantity
            PoQuantity = int.Parse(GridView1.Rows[i].Cells[3].Text);
            unitPrice = decimal.Parse(txtCosprice.Text);
            string batchLot = null;
            batchLot = (string.IsNullOrEmpty(txtBatch.Text) == true) ? null : txtBatch.Text;
            
            if (TextHd.Length == 6)
            {
                objGrnDetail = new GrnDetails();
                objGrnDetail.GrnId = GRNId;

                objGrnDetail.DrugId = DrugId;
                objGrnDetail.Quantity = Quantity;
                objGrnDetail.PoQuantity = PoQuantity;
                objGrnDetail.UnitPrice = unitPrice;
                objGrnDetail.BatchLot = batchLot;
                objGrnDetail.ExpDate = expDate;

                DataRepository.GrnDetailsProvider.Insert(objGrnDetail);
                TotalPoQuantity += PoQuantity;
                TotalQuantity += Quantity;
                Amount = Quantity * unitPrice;
                TotalAmount += Amount;

                // cap nhat lại quantity Po
                objPoDetail = DataRepository.PoDetailsProvider.GetByPoDetailId(poDetailId);
                objPoDetail.RemainQuantity = PoQuantity - Quantity;
                DataRepository.PoDetailsProvider.Update(objPoDetail);

                // update costprice and sellprice DrugDispo
                DrugDispo objDrugDispo = DataRepository.DrugDispoProvider.GetByDrugId(DrugId);
                if (objDrugDispo.CostPrice != decimal.Parse(txtCosprice.Text))
                    objDrugDispo.CostPrice = decimal.Parse(txtCosprice.Text);
                if (objDrugDispo.SellPrice != decimal.Parse(txtSellPrice.Text))
                    objDrugDispo.SellPrice = decimal.Parse(txtSellPrice.Text);
                DataRepository.DrugDispoProvider.Update(objDrugDispo);
            }
            //else
            //{
            //    DataRepository.PoDetailsProvider.Delete(poDetailId);
            //}


        }
        //Grn objGrn = DataRepository.GrnProvider.GetByGrnId(GRNId);
        //objGrn.Amount = Amount;
        //DataRepository.GrnProvider.Update(objGrn);
        Response.Redirect("GrnEdit.aspx?GrnId=" + GRNId, true);
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Retrieve the LinkButton control from the first column.
          //  LinkButton addButton = (LinkButton)e.Row.FindControl("AddButton");
            TextBox txtSellPrice = (TextBox)e.Row.FindControl("TxtSellPrice");
            txtSellPrice.Attributes.Add("readonly", "readonly");
            // Set the LinkButton's CommandArgument property with the
        }


      

    }
}
